package com.dong.controller;

import com.dong.pojo.Book;
import com.dong.pojo.Reader;
import com.dong.pojo.Record;
import com.dong.pojo.User;
import com.dong.service.BookService;
import com.dong.service.ReaderService;
import com.dong.service.RecordService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
public class ReaderController {

    @Autowired
    BookService bookService;

    @Autowired
    RecordService recordService;

    @Autowired
    ReaderService readerService;

    //图书信息
    @RequestMapping("/reader/book/books")
    public String books(Model model){
        List<Book> books = bookService.selectAllBook();
        model.addAttribute("books",books);
        return "user/books";
    }

    //图书详情
    @RequestMapping("/reader/book/info/{bookId}")
    public String info(@PathVariable("bookId") int bookId, Model model){
//        System.out.println("info==>" + bookId);
        Book book = bookService.selectBookById(bookId);
        model.addAttribute("book",book);
        return "user/book_info";
    }

    //到达借书页面
    @RequestMapping("/reader/book/toLend/{bookId}")
    public String toLend(@PathVariable("bookId") int bookId, Model model){
        Book book = bookService.selectBookById(bookId);
        model.addAttribute("book",book);
        return "user/book_lend";
    }

    //借书
    @RequestMapping("/reader/book/lend/{bookId}/{username}/{state}")
    public String lend(@PathVariable("bookId") int bookId,@PathVariable("username") String username,@PathVariable("state") int state,int days,Model model){
        Book book = bookService.selectBookById(bookId);
        if(state == 0){
            model.addAttribute("msg","该书缺货");
            model.addAttribute("book",book);
            return "user/book_lend";
        }else {
            book.setState(0);
            Record record = new Record();
            Date date = new Date();
            record.setBookId(bookId);
            record.setUsername(username);
            record.setLendDate(date);
            record.setBackDate(new Date(date.getTime() + days * 86400000));
            recordService.insertRecord(record);
            bookService.updateBook(book);
            return "redirect:/reader/record/records/" + username;
        }
    }

    //借阅记录
    @RequestMapping("/reader/record/records/{username}")
    public String records(@PathVariable("username") String username,Model model){
        List<Record> records = recordService.selectRecordByUsername(username);
        model.addAttribute("records",records);
        return "user/ownRecord";

    }

    //还书
    @RequestMapping("/reader/record/backBook/{sernum}/{bookId}/{username}")
    public String backBook(@PathVariable("sernum") int sernum,@PathVariable("bookId") int bookId,@PathVariable("username") String username){
        Book book = bookService.selectBookById(bookId);
        book.setState(1);
        bookService.updateBook(book);
        recordService.deleteReocrd(sernum);
        return "redirect:/reader/record/records/" + username;
    }

    //搜索图书信息
    @RequestMapping("/reader/book/searchBook")
    public String searchBook(String keyword,Model model){
        String key = "%" + keyword + "%";
        List<Book> books = bookService.selectLike(key);
        model.addAttribute("books",books);
        return "user/books";
    }

    //到达修改密码页面
    @RequestMapping("/reader/pwd/toChangePassword")
    public String toChangePassword(){
        return "user/password";
    }

    //异步检查密码是否正确
    @RequestMapping("/reader/pwd/checkPassword")
    @ResponseBody
    public String check(String pwd){
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        String username = (String) session.getAttribute("username");
        Reader reader = readerService.selectReaderByUsername(username);
        if (reader.getPassword().equals(pwd)){
            return "true";
        }else {
            return "false";
        }
    }

    //修改密码
    @RequestMapping("/reader/pwd/changePassword/{username}")
    public String changePassword(String checkPassword,@PathVariable("username") String username,Model model){
        Reader reader = readerService.selectReaderByUsername(username);
        reader.setPassword(checkPassword);
        readerService.updateReader(reader);
        model.addAttribute("msg","密码修改成功！");
        return "user/password";
    }

    //个人信息
    @RequestMapping("/reader/info/toReaderInfo/{username}")
    public String toReaderInfo(@PathVariable("username") String username,Model model){
        Reader reader = readerService.selectReaderByUsername(username);
        model.addAttribute("reader",reader);
        return "user/reader_info";
    }
}
